/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.section;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.Transition;

import com.cea.papyrus.core.editpart.IUMLElementEditPart;
import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;
import com.cea.papyrus.tabbedproperties.composites.TriggersComposite;
import com.cea.papyrus.umlutils.Element;

/**
 * Property view section to deal with the list of Triggers of a Transition or a State.
 * 
 * @author David Servat
 * @since 5 apr. 2007
 */
public class TriggerSection extends PropertyViewSection{
	
	/**
	 * Dedicated composite to display list of Triggers.
	 */
	private TriggersComposite triggersComposite;
	
    /**
     * Create contents.
     * 
     * @param parent Composite
     * @param tabbedPropertySheetPage TabbedPropertySheetPage
     */
    @Override
	public void createControls(Composite parent, TabbedPropertySheetPage tabbedPropertySheetPage){
        super.createControls(parent, tabbedPropertySheetPage);
                
        //set a grid Layout
        GridLayout layout = new GridLayout();
        layout.numColumns = 2;
        layout.makeColumnsEqualWidth = true;
        //remove additional space
        layout.marginWidth = 0;
        layout.marginHeight = 0;
        parent.setLayout(layout);
        
        GridData data = new GridData();
		data.grabExcessHorizontalSpace = true;
		data.horizontalAlignment = SWT.FILL;
		
		//table for Triggers
        triggersComposite = new TriggersComposite("Triggers");
        triggersComposite.createContent(parent, tabbedPropertySheetPage.getWidgetFactory());
        triggersComposite.setLayoutData(data);
     }
    
    /**
     * Pass on the selected input to sub composites.
     * 
     * @param part IWorkbenchPart.
     * @param selection ISelection.
     */
    @Override
	public void setInput(IWorkbenchPart part, ISelection selection){
        super.setInput(part, selection);
        
        Assert.isTrue(selection instanceof IStructuredSelection);
        Object input = ((IStructuredSelection)selection).getFirstElement();
        
        if(input instanceof IUMLElementEditPart){
        	org.eclipse.uml2.uml.Element element = ((IUMLElementEditPart)input).getUmlElement();
        	if(element instanceof Transition){
           		Element utilElement = new Element((Transition)element);
        		triggersComposite.setElement(utilElement);
         	}
        }
    }


    /**
     * Refreshes contents.
     */
    @Override
	public void refresh(){
    	triggersComposite.refresh();
     }

    /**
     * Disposes of contents. 
     */
    @Override
    public void dispose(){
    	super.dispose();
    	
    	if(triggersComposite != null)
    		triggersComposite.dispose();
    }
}

